package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseActivity;
import com.turing.usdtpay.entity.LoginData;
import com.turing.usdtpay.manager.UserManager;
import com.turing.usdtpay.mvp.contract.ChangePwdContract;
import com.turing.usdtpay.mvp.presenter.ChangePwdPresent;
import com.turing.usdtpay.mvp.view.BaseTitleView;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.ToastUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/16 17:18
 * @描述：
 **/
public class ChangePwdActivity extends Activity implements View.OnClickListener ,ChangePwdContract.View {
    private BaseTitleView mTitle;
    private Button mCommit;
    private EditText mPwd,mNewPwd,mConfirmPwd;
    private ChangePwdContract.Present mPresent;
    private String mAccount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        initView();
        initTitle();
        initData();
        setPresenter(new ChangePwdPresent(this));
    }

    private void initData() {
        LoginData loginDate = UserManager.getInstance().getLoginDate();
        if (loginDate != null){
            mAccount = loginDate.getAccount();
        }
    }

    private void initTitle() {
        mTitle.setLeftShow(true);
        mTitle.setLeftButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle.setTitle("修改登录密码");
    }

    private void initView() {
        mTitle = (BaseTitleView) findViewById(R.id.title);
        mCommit = (Button) findViewById(R.id.btn_commit);
        mPwd = (EditText) findViewById(R.id.et_pwd);
        mNewPwd = (EditText) findViewById(R.id.et_new_pwd);
        mConfirmPwd = (EditText) findViewById(R.id.et_confirm_pwd);
        mCommit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mCommit.getId()){
            if (TextUtils.isEmpty(mPwd.getText().toString())){
                ToastUtils.showNormolToast(this,"请输入原密码");
                return;
            }
            if (TextUtils.isEmpty(mNewPwd.getText().toString())&&TextUtils.isEmpty(mConfirmPwd.getText().toString())){
                ToastUtils.showNormolToast(this,"请输入新的密码");
                return;
            }
            if (!mNewPwd.getText().toString().equals(mConfirmPwd.getText().toString())){
                ToastUtils.showNormolToast(this,"两个新密码不一致");
                return;
            }

            if (!FormatUtils.checkPasswordRule(mNewPwd.getText().toString())||!FormatUtils.checkPasswordRule(mConfirmPwd.getText().toString())){
                ToastUtils.showNormolToast(this,"密码格式为6-16位字符，需同时包含大小写字母和数字，可包含特殊字符");
                return;
            }

            mPresent.changePwd(this,mAccount,mPwd.getText().toString(),mNewPwd.getText().toString());
        }
    }

    @Override
    public void changeSuccess() {
        Intent intent = new Intent();
        setResult(1,intent);
        ToastUtils.showNormolToast(this,"修改成功");
        finish();
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(this,msg);
    }

    @Override
    public void setPresenter(ChangePwdContract.Present presenter) {
        mPresent = presenter;
    }
}
